<?php

namespace app\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Query\Builder as QueryBuilder;

class DBServiceProvider extends ServiceProvider
{
    public function boot()
    {
        QueryBuilder::macro('format', function ($index = 'id', $to_array = false) {
            return array_reduce($this->get()->all(), function ($res, $next) use ($index, $to_array) {
                $res[$next->$index] = $to_array ? (array)$next : $next;
                return $res;
            }, []);
        });
    }
}
